//
//  GroupGCDViewController.m
//  ObjcGoNew
//
//  Created by 邢伟新 on 2021/4/15.
//

#import "GroupGCDViewController.h"
#import <Masonry/Masonry.h>
#import "ApiServer.h"

@interface GroupGCDViewController ()

@end

@implementation GroupGCDViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"GCD-Group";
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIButton *barrierButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [barrierButton setTitle:@"按钮" forState:(UIControlStateNormal)];
    [barrierButton addTarget:self action:@selector(groupEnterAndLeave) forControlEvents:(UIControlEventTouchUpInside)];
    barrierButton.backgroundColor =[UIColor purpleColor];
    barrierButton.tag = 1001;
    [self.view addSubview:barrierButton];
    [barrierButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.height.mas_equalTo(50);
        make.top.mas_equalTo(150);
    }];
    
    
    UIButton *maxButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [maxButton setTitle:@"网络请求" forState:(UIControlStateNormal)];
    [maxButton addTarget:self action:@selector(netWork3) forControlEvents:(UIControlEventTouchUpInside)];
    maxButton.backgroundColor =[UIColor purpleColor];
    maxButton.tag = 1001;
    [self.view addSubview:maxButton];
    [maxButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.height.mas_equalTo(50);
        make.top.mas_equalTo(barrierButton.mas_bottom).offset(15);
    }];

}


-(void)groupEnterAndLeave{
    
    NSLog(@"Group-Begin");
    dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
    dispatch_group_t group = dispatch_group_create();
    
    dispatch_group_enter(group);
    dispatch_async(queue, ^{
    
        //模拟耗时操作
        [NSThread sleepForTimeInterval:5];
        NSLog(@"1-------%@",[NSThread currentThread]);
        dispatch_group_leave(group);
        
    });
    
    dispatch_group_enter(group);
    dispatch_async(queue, ^{
        //模拟耗时操作
        [NSThread sleepForTimeInterval:2];
        NSLog(@"2-------%@",[NSThread currentThread]);
        dispatch_group_leave(group);
    });
    
    dispatch_group_notify(group, queue, ^{
        
        
        NSLog(@"3-------%@",[NSThread currentThread]);
        NSLog(@"Group-end");
        
    });
    
    
    
    
    
}



//groupWait
-(void)groupWait{
    
    NSLog(@"1--------------%@",[NSThread currentThread]);
    NSLog(@"group-begin");
    
    dispatch_group_t group = dispatch_group_create();
    dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
    
    dispatch_group_async(group, queue, ^{
    
        //模拟耗时操作
        [NSThread sleepForTimeInterval:6];
        NSLog(@"2--------------%@",[NSThread currentThread]);
        
    });
    
    dispatch_group_async(group, queue, ^{
    
        //模拟耗时操作
        [NSThread sleepForTimeInterval:3];
        NSLog(@"3--------------%@",[NSThread currentThread]);
        
    });
    
    //当组内所有的任务执行完成后才继续执行 (会阻塞当前进程)
    dispatch_group_wait(group, DISPATCH_TIME_FOREVER);

    NSLog(@"group-end");
    
    
}





//基本用法
-(void)groupNotify{
    
    NSLog(@"1--------------%@",[NSThread currentThread]);
    NSLog(@"group-begin");
    
    dispatch_group_t group = dispatch_group_create();
    dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
    
    dispatch_group_async(group, queue, ^{
    
        //模拟耗时操作
        [NSThread sleepForTimeInterval:6];
        NSLog(@"2--------------%@",[NSThread currentThread]);
        
    });
    
    dispatch_group_async(group, queue, ^{
    
        //模拟耗时操作
        [NSThread sleepForTimeInterval:3];
        NSLog(@"3--------------%@",[NSThread currentThread]);
        
    });
    
    dispatch_group_notify(group, queue, ^{
        
        NSLog(@"4--------------%@",[NSThread currentThread]);
        
    });
    
    NSLog(@"group-end");
    

}

//a b c 三个网络请求结束后,执行刷新UI操作
-(void)netWork3{
    
    dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
    dispatch_group_t group = dispatch_group_create();
    
    dispatch_group_enter(group);
    
    dispatch_group_async(group, queue, ^{
        //创建信号量
        
        NSLog(@"begin1----%@",[NSThread currentThread]);
        
        [ApiServer GeiWanApiSuccess:^(NSDictionary * _Nonnull resultDic) {
        
            NSLog(@"end1----%@",[NSThread currentThread]);
            dispatch_group_leave(group);
          
            
        } failure:^(NSError * _Nonnull error) {
        
            
            NSLog(@"end1----%@",[NSThread currentThread]);
            dispatch_group_leave(group);
        }];
        
       
    });
    
    dispatch_group_enter(group);
    
    dispatch_group_async(group, queue, ^{
        //创建信号量
        
        NSLog(@"begin2----%@",[NSThread currentThread]);
        
        [ApiServer GeiWanApiSuccess:^(NSDictionary * _Nonnull resultDic) {
        
            NSLog(@"end2----%@",[NSThread currentThread]);
            dispatch_group_leave(group);
          
            
        } failure:^(NSError * _Nonnull error) {
        
            
            NSLog(@"end2----%@",[NSThread currentThread]);
            dispatch_group_leave(group);
        }];
        
       
    });
    
    dispatch_group_enter(group);
    
    dispatch_group_async(group, queue, ^{
        //创建信号量
        
        NSLog(@"begin3----%@",[NSThread currentThread]);
        
        [ApiServer GeiWanApiSuccess:^(NSDictionary * _Nonnull resultDic) {
        
            NSLog(@"end3----%@",[NSThread currentThread]);
            dispatch_group_leave(group);
          
            
        } failure:^(NSError * _Nonnull error) {
        
            
            NSLog(@"end3----%@",[NSThread currentThread]);
            dispatch_group_leave(group);
        }];
        
       
    });
    
    dispatch_group_notify(group, queue, ^{
        dispatch_async(dispatch_get_main_queue(), ^{
            NSLog(@"刷新UI---%@",[NSThread currentThread]);
        });
    });
    
    
    
    
    
}




@end
